package union;

/**
 * @Author: LDeng
 * @Date: 2021-03-31 14:03
 */
public class UnionFind_QU extends UnionFind {
    public UnionFind_QU(int capacity) {
        super(capacity);
    }
    //查找根节点
    @Override
    public int find(int v) {
        rangeCheck(v);
        while(v!=parents[v]){
            v=parents[v];
        }
        return v;
    }

    //合并v1,v2所在的集合
    //将v1的根节点合并到v2的根节点
    @Override
    public void union(int v1, int v2) {
        int p1=find(v1);
        int p2=find(v2);
        if(p1==p2) return;
        parents[p1]=parents[p2];
    }
}
